package com.mylogistics.controller;

import com.alibaba.excel.EasyExcel;
import com.mylogistics.pojo.User;
import com.mylogistics.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * 此类仅仅用于实验表格生成
 */
@RestController
public class EasyExcelTestController {
    @Autowired
    UserService userService;
    @GetMapping("download")
    public void download(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

        //输出excel
        EasyExcel.write(
                response.getOutputStream(),
                User.class
        ).sheet("角色信息").doWrite(userService.list());
    }
}
